package com.iims.common.vo;

import cn.hutool.core.util.StrUtil;
import com.iims.common.core.domain.HttpParamMap;
import com.iims.common.core.page.SearchTimeSupport;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

/**
 * @author fanpei
 * @date 2021/7/14 17:16
 */
@Data
public abstract class GroupSearchBase extends HttpParamMap {

    @ApiModelProperty(value = "时间搜索:显示和值来源于字典数据business_common_searchTimeRange", required = true)
    private int timeValue = -1;

    @ApiModelProperty(value = "综合模糊搜索")
    private String searchValue;

    /**
     * 产生sql时间参数
     */
    public void generateSqlArgs() {
        if (timeValue != -1) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Map condition = getParams();
            Calendar calendar = Calendar.getInstance();
            calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(calendar.MINUTE, 59);
            calendar.set(Calendar.MILLISECOND, 59);
            condition.put(SearchTimeSupport.END_TIME, df.format(calendar.getTime()));
            switch (timeValue) {
                case 0://近一年
                    calendar.set(Calendar.HOUR_OF_DAY, -8640);
                    break;
                case 1://近两年
                    calendar.set(Calendar.HOUR_OF_DAY, -17280);
                    break;
                case 2://近三年
                    calendar.set(Calendar.HOUR_OF_DAY, -25920);
                    break;
                case -1:
                default://全部时间
                    break;
            }
            calendar.set(calendar.MINUTE, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            condition.put(SearchTimeSupport.START_TIME, df.format(calendar.getTime()));
        }
        initCurrentUser();
    }

    /**
     * 当前用户(user_code)
     */
    @ApiModelProperty(hidden = true)
    protected String currentUser;

    /**
     * 当前部门(user_code)
     */
    @ApiModelProperty(hidden = true)
    protected Long currentDept;

    @ApiModelProperty(hidden = true)
    private String likeSearchValue;

    @ApiModelProperty(hidden = true)
    @ApiIgnore
    private String getLikeSearchValue() {
        if (StrUtil.isEmpty(likeSearchValue)) {
            likeSearchValue = StrUtil.join("%", searchValue, "%");
        }
        return likeSearchValue;
    }

    /**
     * 初始化当前用户
     */
    public abstract void initCurrentUser();
}
